package cn.ctyun.ipush.service.impl;

import cn.ctyun.ipush.dao.ExcelDao;
import cn.ctyun.ipush.dao.TaskDao;
import cn.ctyun.ipush.model.ExcelModel;
import cn.ctyun.ipush.service.ExcelService;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.ExcelVo;
import com.dexcoder.commons.bean.BeanConverter;
import com.dexcoder.commons.utils.UUIDUtils;
import com.dexcoder.dal.build.Criteria;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-7-11
 * Time: 上午9:45
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ExcelServiceImpl implements ExcelService {


    @Autowired
    private ExcelDao excelDao;

    @Autowired
    private TaskDao taskDao;

    @Override
    public void save(ExcelVo excelVo) {
        excelVo.setExcelModelId(UUIDUtils.getUUID32());
        excelVo.setCreateDate(DateUtils.dateToString(new Date()));
        ExcelModel excelModel = excelVo.getTargetObject(ExcelModel.class);
        //保存到数据库中
        save(excelModel);
    }

    @Override
    public String save(ExcelModel excelModel) {
        excelModel.setCreateDate(DateUtils.dateToString(new Date()));
        excelModel.setIsDeleted("0");
        excelModel.setExcelModelId(UUIDUtils.getUUID32());
        excelDao.save(excelModel);
        return excelModel.getExcelModelId();
    }

    @Override
    public int delete(String excelId) {
        //判断是否存在引用，是否在task中.如果有引用肯定存在Job，此时不删除；
        if (taskDao.isExistExcelInTask(excelId)) {
            return 0;
        }
        //不存在，删除缓存，数据库记录删除；无引用肯定也不存在对应的Job
        // DBUtils.deleteConnectionCache(dataSourceDao.querySingleResult(dataSourceId));
        //数据库记录软删除
        return excelDao.delete(excelId);
    }

    @Override
    public int update(ExcelVo excelVo) {
        if (StringUtils.isEmpty(excelVo.getExcelModelId())) {
            throw new RuntimeException("Excel 主键ID 不存在");
        }
        //todo 额外处理
        ExcelModel excelModel = excelVo.getTargetObject(ExcelModel.class);
        ExcelModel excelModelDb = querySingleResult(excelModel.getExcelModelId());

        if (excelModel.getExcelName() != null)
            excelModelDb.setExcelName(excelModel.getExcelName());
        if (excelModel.getExcelPath() != null)
            excelModelDb.setExcelPath(excelModel.getExcelPath());

        return excelDao.update(excelModelDb);
    }

    @Override
    public ExcelVo querySingleResultVo(String excelId) {
        ExcelModel excelModel = querySingleResult(excelId);
        return excelModel.getTargetObject(ExcelVo.class);
    }

    @Override
    public ExcelModel querySingleResult(String excelId) {
        return excelDao.querySingleResult(excelId);
    }

    @Override
    public List<ExcelModel> queryList(ExcelModel excelModel) {
        Criteria criteria = Criteria.select(ExcelModel.class).where("excelName", "  like ", new Object[]{"%" + excelModel.getExcelName() + "%"}).where("and isDeleted", new Object[]{"0"});
        return excelDao.queryList(criteria);
    }

    @Override
    public List<ExcelVo> queryListVo(ExcelVo excelVo) {
        List<ExcelModel> excelModelList = queryList(excelVo.getTargetObject(ExcelModel.class));
        List<ExcelVo> excelVoList = BeanConverter.convert(ExcelVo.class, excelModelList);
        return excelVoList;
    }

    @Override
    public boolean isExist(ExcelVo excelVo) {
        ExcelModel excelModel = excelVo.getTargetObject(ExcelModel.class);
        if (null == excelDao.querySingleResultByName(excelModel.getExcelName())) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<ExcelVo> queryAll() {
        List<ExcelModel> excelModelList = excelDao.queryAll(new ExcelModel());
        List<ExcelVo> excelVoList = BeanConverter.convert(ExcelVo.class, excelModelList);
        return excelVoList;
    }

    @Override
    public List<ExcelVo> queryListVoByPage(ExcelVo excelVo) {
        //分页查询
        int currentPage = excelVo.getCurPage();
        int itemPages = excelVo.getItemsPerPage();
        List<ExcelModel> excelModelList = excelDao.queryListByPage(currentPage, itemPages, excelVo);
        List<ExcelVo> excelVoList = BeanConverter.convert(ExcelVo.class, excelModelList);
        return excelVoList;
    }
}
